package com.liyuxiang.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyuxiang.yygh.cmn.client.DictFeignClient;
import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.hosp.mapper.HospitalSetMapper;
import com.liyuxiang.yygh.hosp.repository.HospitalRepository;
import com.liyuxiang.yygh.hosp.service.HospitalService;
import com.liyuxiang.yygh.model.hosp.Hospital;
import com.liyuxiang.yygh.model.hosp.HospitalSet;
import com.liyuxiang.yygh.model.hosp.Schedule;
import com.liyuxiang.yygh.vo.hosp.HospitalQueryVo;
import com.liyuxiang.yygh.vo.order.SignInfoVo;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl  implements HospitalService {
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    DictFeignClient dictFeignClient;

    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        String jsonString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        String hoscode = hospital.getHoscode();
        //先判断是否有相同的数据
        Hospital hospitalExist = hospitalRepository.getHospitalRepositoryByHoscode(hoscode);

        if (!ObjectUtils.isEmpty(hospitalExist)) {
            //如果存在则更新
            hospital.setHoscode(hospitalExist.getHoscode());
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        } else {
            //如果不存在则添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        }
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalRepositoryByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> getHospitalPage(Integer current, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建Pageable，设置当前页和记录数
        Pageable pageable = PageRequest.of(current - 1, limit);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //创建Example对象
        ExampleMatcher matcher = ExampleMatcher.matching();
        matcher.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> page = hospitalRepository.findAll(example, pageable);
        page.getContent().stream().forEach(i -> {
            String hostype = dictFeignClient.getDictName("Hostype", i.getHostype());
            String provinceCode = dictFeignClient.getDictName(i.getProvinceCode());
            String cityCode = dictFeignClient.getDictName(i.getCityCode());
            String districtCode = dictFeignClient.getDictName(i.getDistrictCode());
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("hostypeString", hostype);
            paramMap.put("fullAddress", provinceCode + cityCode + districtCode);
            i.setParam(paramMap);
        });
        return page;
    }

    @Override
    public Boolean updateHospStatusById(String id, int status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        if (!ObjectUtils.isEmpty(hospital)) {
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
            return true;
        }
        return false;
    }

    @Override
    public Hospital getHospDetailById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.getParam().put("fullAddress", dictFeignClient.getDictName(hospital.getProvinceCode()) + dictFeignClient.getDictName(hospital.getCityCode()) + dictFeignClient.getDictName(hospital.getDistrictCode()));
        return hospital;
    }

    @Override
    public List<Hospital> getHospitalByHosName(String hosName) {
        List<Hospital> list = hospitalRepository.getHospitalByHosnameLike(hosName);
        return list;
    }
}
